package club.yuit.desktop.ui.brokers;

import club.yuit.desktop.layout.WrapLayout;
import club.yuit.rmq.model.BrokerDetails;
import club.yuit.rmq.service.RMQClusterService;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;

/**
 * @author: yuit
 * @date: 2023/12/04 17:30
 */
@Slf4j
public class BrokerListPanel  extends JPanel {

    private JTable table;
    private RMQClusterService service;


    private JComboBox<String> clusterSelectBox;


    private Map<String,List<BrokerDetails>> brokerList;

    private JPanel contentPanel;

    private String currentCluster;






    public BrokerListPanel(RMQClusterService service) {
        this.service = service;

       this.setLayout(new BorderLayout());
        contentPanel = new JPanel();
        contentPanel.setLayout(new WrapLayout(WrapLayout.LEFT));
        JScrollPane scrollPane = new JScrollPane(contentPanel) ;
        scrollPane.setBorder(null);


        this.add(controlPanel(),BorderLayout.NORTH);
        this.add(scrollPane,BorderLayout.CENTER);

        changeCluster();
        updateData();
    }

    private JPanel controlPanel(){

        JPanel panel = new JPanel();
        BoxLayout l = new BoxLayout(panel, BoxLayout.Y_AXIS);
        panel.setLayout(l);

        JPanel control = new JPanel(new FlowLayout(FlowLayout.LEFT));

        JButton button = new JButton("刷新");
        clusterSelectBox = new JComboBox<>();
        clusterSelectBox.addItem("----数据加载中-----");
        control.add(clusterSelectBox);
        control.add(button);

        button.addActionListener(e -> {
           updateData();
        });


        clusterSelectBox.addItemListener(e->{
            if (e.getStateChange()== ItemEvent.SELECTED){
                currentCluster = (String) e.getItem();
                if (currentCluster.equals("全部")){
                    currentCluster = null;
                }
                changeSelectCluster(currentCluster);
            }
        });

        panel.add(control);
        panel.add(new JSeparator());

        return panel;
    }


    public void changeCluster(){
        try {
            clusterSelectBox.removeAllItems();
            Set<String> clusterList = service.getClusterList();
            clusterSelectBox.addItem("全部");
            clusterList.forEach(v-> clusterSelectBox.addItem(v));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public void updateData()  {

        try {
           this.brokerList = this.service.brokerList();
           displayData();
           changeSelectCluster(currentCluster);
           this.updateUI();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }


    private void displayData(){

        if (brokerList!=null){
            contentPanel.removeAll();
            brokerList.forEach((k,v)->{
                if (!v.isEmpty()){
                    v.forEach(t->{
                        contentPanel.add(BrokerCardPanel.create(t));
                    });

                }
            });
        }

    }

    public void changeSelectCluster(String cluster){
        Component[] components = contentPanel.getComponents();
        for (Component component : components) {
            if (component instanceof BrokerCardPanel bp){
                if (cluster==null){
                    bp.setVisible(true);
                    continue;
                }
                String c = bp.getDetails().getCluster();
                boolean equals = c.equals(cluster);
                bp.setVisible(equals);
            }
        }
    }


    @Override
    public Insets getInsets() {
        return new Insets(0, 10, 10, 10);
    }
}
